package com.javaweb.secondhand_trading.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.File;
import java.nio.file.Paths;
@Configuration
public class WebConfig implements WebMvcConfigurer {



    private static final String IMAGE_PATH = Paths.get(System.getProperty("user.dir"))
            .getParent()  // 获取上一级目录
            .resolve("Java_Basic/uploads")  // 拼接 uploads 目录
            .toString() + "/";  // 转换为字符串并添加斜杠

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 确保目录存在
        File uploadDir = new File(IMAGE_PATH);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        // 配置静态资源映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + IMAGE_PATH)
                .setCachePeriod(3600) // 缓存一小时
                .resourceChain(true); // 开启资源链优化

        System.out.println("图片存储路径: " + IMAGE_PATH);
        System.out.println("资源映射配置完成: /uploads/** -> " + IMAGE_PATH);
    }
}